package com.zhangdy.agent.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangdy.agent.common.dto.LoadAgent;
import com.zhangdy.agent.common.dto.ReturnT;
import com.zhangdy.agent.common.utils.AgentRemotingUtil;
import com.zhangdy.agent.entity.MethodPoint;
import com.zhangdy.agent.exception.AgentException;
import com.zhangdy.agent.mapper.MethodPointMapper;
import com.zhangdy.agent.service.MethodPointService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhangdy
 * @since 2022-01-06
 */
@Service
public class MethodPointServiceImpl extends ServiceImpl<MethodPointMapper, MethodPoint> implements MethodPointService {


    @Value("${agent.jar.path}")
    private String agentJarPath;

    @Override
    public void loadAgent(Long id) {
        MethodPoint methodPoint = this.getById(id);
        String serverPath = methodPoint.getServerPath();
        if (serverPath == null || serverPath.trim().length() == 0) {
            serverPath = "";
        }
        String loadAgentUrl = "http://" + methodPoint.getServerIp() + ":" + methodPoint.getServerPort() + "/" + serverPath + "/loadAgent";
        LoadAgent loadAgent = new LoadAgent();
//        loadAgent.setPath("/data/iccagent/icc-agent-core-0.0.1-SNAPSHOT.jar");
        loadAgent.setPath(agentJarPath);
//        loadAgent.setPath("/data/zhangsan/zhangdy-agent-core-0.0.1-SNAPSHOT.jar");
        ReturnT returnT = AgentRemotingUtil.postBody(loadAgentUrl, loadAgent,
                String.class);

        if (returnT.getCode() != ReturnT.SUCCESS_CODE) {
            throw new AgentException(" load agent exception ");

        }

    }
}
